package com.wjjun.basemvpvm.utils;

import android.text.TextUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

/**
 * 时间转换工具
 * Created by 67535 on 2018/5/3.
 */
public class TimeUtils {

    private static final String DEF_FORMAT = "yyyy-MM-dd HH:mm:ss";

    /**
     * 转换时间
     */
    public static String formatDate(long time, String format) {
        Date date = new Date(time);
        if (TextUtils.isEmpty(format)) {
            format = DEF_FORMAT;
        }
        SimpleDateFormat formatter = new SimpleDateFormat(format);
        return formatter.format(date);
    }

    /**
     * 转换时间
     */
    public static String formatDateBySecond(long second, String format) {
        Date date = new Date(second * 1000L);
        if (TextUtils.isEmpty(format)) {
            format = DEF_FORMAT;
        }
        SimpleDateFormat formatter = new SimpleDateFormat(format);
        return formatter.format(date);
    }

    public static int getData(String time, String format) {
        SimpleDateFormat formatter = new SimpleDateFormat(format);
        try {
            Date date = formatter.parse(time);
            return (int) (date.getTime() / 1000L);
        } catch (ParseException e) {
            e.printStackTrace();
            return 0;
        }
    }

}
